//
//  ZDLeftDrawerController.m
//  ZDPension
//
//  Created by 刘甲奇 on 2017/6/13.
//  Copyright © 2017年 maker. All rights reserved.
//

#import "ZDLeftDrawerController.h"
#import "ZDSettingCell.h"
#import "ZDResignCell.h"
#import "MakerTools.h"
#import "ZDOldManModel.h"

#import "ZDLoginController.h"
#import "ZDNavigationController.h"
#import "ZDAddressListController.h"
#import "ZDEditPasswordController.h"
#import "ZDMyWalletController.h"

static NSString *const ZDSettingCellId = @"ZDSettingCell";
static NSString *const ZDResignCellId = @"ZDResignCell";

@interface ZDLeftDrawerController ()

// 图片数据源
@property(nonatomic,strong)NSArray* imageArray;
// 标题数据源
@property(nonatomic,strong)NSArray* titleArray;

@end

@implementation ZDLeftDrawerController

- (void)setupUI
{
    [super setupUI];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDSettingCell" bundle:nil] forCellReuseIdentifier:ZDSettingCellId];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDResignCell" bundle:nil] forCellReuseIdentifier:ZDResignCellId];
    self.tableView.rowHeight = 55;
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self resetTablewFrame:self.view.bounds];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:ISLOGIN])
    {
        ZDOldManModel *old = [MakerTools readFromSandBox:OLDMAN_INFO];
        self.title = old.username;
    }else
    {
        self.title = @"未登录";
    }
    
}
- (void)loadData
{
    NSArray *imageArr = @[@"left_wallet",@"manage_receiveAddress",@"edit_password"];
    _imageArray = imageArr;
    
    NSArray *titleArr = @[@"我的钱包",@"收货地址管理",@"修改密码"];
    _titleArray = titleArr;

}

#pragma mark- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.imageArray.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 3)
    {
        ZDResignCell *cell = [tableView dequeueReusableCellWithIdentifier:ZDResignCellId forIndexPath:indexPath];
        return cell;
    }
    ZDSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:ZDSettingCellId forIndexPath:indexPath];
    NSString *imageStr = self.imageArray[indexPath.row];
    cell.icon.image = [UIImage imageNamed:imageStr];
    cell.titleLabel.text = self.titleArray[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (![[NSUserDefaults standardUserDefaults] boolForKey:ISLOGIN])
    {
        [self presentLoginController];
    }
    switch (indexPath.row) {
        case 0:
        {
           ZDLog(@"%s---%d---钱包",__func__,__LINE__);
            //没有登录状态,去登陆
            ZDMyWalletController *wallet = [[ZDMyWalletController alloc] init];
            wallet.isPresent = YES;
            [self presentController:wallet];

        }
            break;
        case 1:
        {
           ZDLog(@"%s---%d---收货地址",__func__,__LINE__);
            ZDAddressListController *address = [[ZDAddressListController alloc] init];
            address.mode = ManageAddress;
            [self presentController:address];
        }

            break;
        case 2:
        {
            ZDLog(@"%s---%d---修改密码",__func__,__LINE__);
            ZDEditPasswordController *password = [[ZDEditPasswordController alloc] init];
            [self presentController:password];
        }
            break;
        case 3:
        {
            ZDLog(@"%s---%d---退出登录",__func__,__LINE__);
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:ISLOGIN];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
            ZDLoginController *login = [[ZDLoginController alloc] init];
            [self presentController:login];
        }
            break;
            
        default:
            break;
    }
}
- (void)presentController:(UIViewController *)controller
{
    dispatch_async(dispatch_get_main_queue(), ^{
        ZDNavigationController *nav = [[ZDNavigationController alloc] initWithRootViewController:controller];
        [self presentViewController:nav animated:YES completion:nil];
    });
}
@end
